package com.bbx.taxi.client.Adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bbx.taxi.client.Bean.Wallect;
import com.bbx.taxi.client.R;

public class PayAdapter extends BaseAdapter {
	public static String AVAILABLE = "available";// 是否可用
	public static String TYPE = "type";// 类型
	public static String TEXT = "text";
	public static String DISCOUNT = "discount";
	public static String TYPE_ALIPAY = "alipay";// 类型支付宝
	public static String TYPE_WEIXIN = "weixin";// 类型weixin
	public static String TYPE_YINLIAN = "yinlian";// 类型银联
	public static String TYPE_YUE= "yue";// 帮邦行余额
	public static String TYPE_LINE = "line";// 现金
	private List<Map<String, String>> list;
	private Context main;
	public Map<Integer, Boolean> isSelected;

	public PayAdapter(Context context, List<Map<String, String>> list) {
		this.list = list;
		main = context;
		init(0);
	}

	// 初始化
	public void init(int id) {
		// 这儿定义isSelected这个map是记录每个listitem的状态，初始状态全部为false。
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < list.size(); i++) {
			if (id == i) {
				isSelected.put(i, true);
			} else {
				isSelected.put(i, false);
			}

		}
	}

	public void notifyDataSetChanged(int id) {
		this.notifyDataSetChanged();
		init(id);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(main);
			convertView = (View) inflater.inflate(R.layout.item_pay, null);
			viewholder = new ViewHolder();
			viewholder.iv_pay = (ImageView) convertView
					.findViewById(R.id.iv_pay);
			viewholder.tv_pay = (TextView) convertView
					.findViewById(R.id.tv_pay);
			viewholder.tv_discount = (TextView) convertView
					.findViewById(R.id.tv_discount);
			viewholder.cb_pay = (CheckBox) convertView
					.findViewById(R.id.cb_pay);
			convertView.setTag(viewholder);
		} else
			viewholder = (ViewHolder) convertView.getTag();
		// 是否可用,主要用于微信
		boolean available = false;
		if (list.get(position).get(AVAILABLE) != null) {
			available = list.get(position).get(AVAILABLE).equals("false") ? false
					: true;
		}
		// 显示图标
		String type = list.get(position).get(TYPE);
		if (type.equals(TYPE_ALIPAY)) {
			viewholder.iv_pay.setImageResource(R.drawable.icon_zfb);
		} else if (type.equals(TYPE_WEIXIN)) {
			if (available) {
				viewholder.iv_pay.setImageResource(R.drawable.icon_wx);
			} else {
				viewholder.iv_pay.setImageResource(R.drawable.icon_wx_not);
				convertView.setClickable(true);
			}

		} else if (type.equals(TYPE_YINLIAN)) {
			viewholder.iv_pay.setImageResource(R.drawable.icon_yhk);
		}else if (type.equals(TYPE_LINE)) {
			viewholder.iv_pay.setImageResource(R.drawable.icon_xj);
		}else if(type.equals(TYPE_YUE)){
			viewholder.iv_pay.setImageResource(R.drawable.icon_ye);
		}

		//如果是余额，则要显示具体的余额
		if(type.equals(TYPE_YUE)){
			viewholder.tv_pay.setText(Html.fromHtml(list.get(position).get(TEXT)+"("+"<font color=\"#FF4343\">"+ Wallect.accountBalance+"元</font>"+")"));
		}else{
			viewholder.tv_pay.setText(list.get(position).get(TEXT));
		}

		int discount = 0;
		try {
			discount = Integer.parseInt(list.get(position).get(DISCOUNT));
		} catch (Exception e) {
			discount = 0;
		}
		// 显示第三方的折扣
		if (discount > 0) {
			viewholder.tv_discount.setVisibility(View.VISIBLE);
			viewholder.tv_discount.setText(String.format(
					main.getString(R.string.end_pay_discount),
					list.get(position).get(DISCOUNT)));
		} else {
			viewholder.tv_discount.setVisibility(View.GONE);
		}
		viewholder.cb_pay.setChecked(isSelected.get(position));
		return convertView;

	}

	public class ViewHolder {
		ImageView iv_pay;
		TextView tv_pay, tv_discount;
		public CheckBox cb_pay;
	}
}